<?php

namespace App\Http\Controllers\Admin;

use App\Services\Admin\RoleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class RoleController extends AdminBaseController
{
    protected $roleService;

    public function __construct(RoleService $roleService)
    {
        $this->roleService = $roleService;
    }

    /**
     * 获取角色列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request): \Illuminate\Http\JsonResponse
    {
        $params = $request->all();
        $data = $this->roleService->list($params);
        return $this->success($data);
    }

    /**
     * 获取角色详情
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function view(int $id): \Illuminate\Http\JsonResponse
    {
        $data = $this->roleService->view($id);
        return $this->success($data);
    }

    /**
     * 创建角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request): \Illuminate\Http\JsonResponse
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|min:2|max:50',
            'description' => 'nullable|string|max:200',
            'status' => 'integer|in:0,1',
            'permissions' => 'nullable|array',
            'permissions.*' => 'integer|exists:permissions,id'
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first(), 422);
        }

        $data = $request->all();
        $id = $this->roleService->create($data);
        return $this->success($id);
    }

    /**
     * 更新角色
     * @param int $id
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(int $id, Request $request): \Illuminate\Http\JsonResponse
    {
        $validator = Validator::make($request->all(), [
            'name' => 'string|min:2|max:50',
            'description' => 'nullable|string|max:200',
            'status' => 'integer|in:0,1',
            'permissions' => 'nullable|array',
            'permissions.*' => 'integer|exists:permissions,id'
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first(), 422);
        }

        $data = $request->all();
        $rows = $this->roleService->update($id, $data);
        return $this->success($rows);
    }

    /**
     * 删除角色
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(int $id): \Illuminate\Http\JsonResponse
    {
        $rows = $this->roleService->delete($id);
        return $this->success($rows);
    }

    /**
     * 更新角色状态
     * @param int $id
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateStatus(int $id, Request $request): \Illuminate\Http\JsonResponse
    {
        $validator = Validator::make($request->all(), [
            'status' => 'required|integer|in:0,1'
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first(), 422);
        }

        $rows = $this->roleService->update($id, ['status' => $request->input('status')]);
        return $this->success($rows);
    }
}
